package com.yoomiito.app.ui.anyview.goodsinfo.bottom

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import com.jakewharton.rxbinding3.view.clicks

import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.interf.BaseBottomBar
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.ui.order.sureorder.singleGoods.SureOrderActivity
import com.yoomiito.app.utils.*
import kotlinx.android.synthetic.main.youpin_bottom_btn.view.*
import java.util.concurrent.TimeUnit

/**
 * 星店商品详情--底部按钮
 */
class StarStoreBottomBar(context: Context, isLogin: Boolean) : BaseBottomBar(context, isLogin) {
    //    private lateinit var mSongYouMiTv:TextView
    init {
        LayoutInflater.from(mContext).inflate(R.layout.youpin_bottom_btn, this, true)
        initView()
    }

    private fun initView() {
        award_ll.visibility = View.GONE
        show_award.visibility = View.GONE
//        mSongYouMiTv = findViewById(R.id.show_youmi)

        bg_3.clicks()
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe {
                    RouterUtils.toShareGoods(mContext as BaseActivity<*>, mGoodsDetail.num_iid.toString(), AllCode.StarStore)
                }
        bg_2.clicks()
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe {
                    (mContext as BaseActivity<*>).showLoading()
                    canBuy(mGoodsDetail.num_iid.toString())
                            .subscribe(object : ApiSubscriber<BaseModel<*>>() {
                                override fun onSuccess(t: BaseModel<*>?) {
                                    (mContext as BaseActivity<*>).dismissLoading()
                                    SureOrderActivity.start(mContext, mGoodsDetail, AllCode.StarStore)
                                }

                                override fun onFail(error: ApiException?) {
                                    (mContext as BaseActivity<*>).dismissLoading()
                                    YMToast.showText(error?.msg)
                                }

                            })

                }
        //        findViewById<View>(R.id.bg_3).setOnClickListener {//分享
//            if (checkClick()) {
//
//            }
//        }
//        findViewById<View>(R.id.bg_2).setOnClickListener {//购买
//            if (checkClick()) {
//                BuyGoodsActivity.start(mContext, mGoodsDetail, AllCode.StarStore)
//            }
//        }
    }


    override fun updateInfo(detail: GoodsDetail) {
        super.updateInfo(detail)
        if (App.mAppConfig.isLogin) {
            show_youmi.text = "送${detail.yo_money}优米"
            show_youmi.visibility = if (detail.yo_money == 0) View.GONE else View.VISIBLE
            if (UserInfoConfig.getRoleId() >= Type.LevelType.VIP_10) {
//            if (!SpannableUtils.isEmptyOr0(detail.get_money)) {
//                mAwardTv!!.text = "返佣 ¥" + detail.get_money
//                mShareAwardTv!!.text = "奖励¥" + detail.get_money
//                mAwardLl!!.visibility = View.VISIBLE
//                mShareAwardTv!!.visibility = View.VISIBLE
//                mShareContentTv!!.text = "分享赚钱"
                share_bottom.text = "分享赚钱"
            } else {
                share_bottom.text = "分享"
//                                mShareContentTv.setText("分享");
                //                mAwardLl.setVisibility(View.GONE);
                //                mShareAwardTv.setVisibility(View.GONE);
            }
        }
    }
}
